package com.chengdu.chun.mapper;

import com.chengdu.chun.po.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * 对应映射UserMapper.xml文件的接口
 * xml文件中所有的操作定义都要有一个对应id名称的方法
 */
@Repository
public interface UserMapper {

    @Update("update user set pass=#{pass} where uid = #{uid}")
    public int updatePassword(@Param("uid") Integer uid, @Param("pass") String pass);

    /**
     * 根据用户id查询用户信息，该方法名称对应xml文件中的某个查询标记的id
     *
     * @param uid
     * @return
     */
    @Select("select * from user where uid = #{id}")
    public User selectUserById(int uid);

    /**
     * 如果是多参数，那么必须使用@Param注解进行匹配定义
     *
     * @param name
     * @param pass
     * @return
     * @Param注解中的名称必须和SQL语句语句中的站位定义名称完全一致
     */
    // 注解@results相当于在XML中的resultmap
    @Results(id = "loginuser", value = {
            @Result(column = "uid", property = "uid"),
            // 注解中通过一个查询获取操作结构
            /**
             * property作为接收结果的属性，column作为一个参数列传递需要的数值过去，
             *  one去通过@One注解提供一个全名路径的接口方法，执行查询并返回结果
             */
//            @Result(property = "classesnum", column = "uid",
//                    one = @One(select = "com.chengdu.chun.mapper.ClassesMapper.selectCountByTeacher")),
//            @Result(property = "studentnum", column = "uid",
//                    one = @One(select = "com.chengdu.chun.mapper.StudentMapper.selectCountByTeacher"))
    })
    @Select("select * from user where uname=#{username} and pass=#{userpass} and ustatus = 1")
    public User loginByNameAndPass(@Param("username") String name, @Param("userpass") String pass);

    @Insert("insert into user values(null,#{username},#{userpass},#{typeid},#{sex},#{birthday},#{mobile},#{address},1,now())")
    public int insertUser(User user);

    int deleteByPrimaryKey(Integer uid);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer uid);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}